/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package DAO;

import DTO.CFields;
import DTO.CSanPham;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
public class CSanPhamDAO extends IData<CSanPham>{
    public CSanPhamDAO(){}

    @Override
    protected Object OnSubmit(CSanPham item, int t, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    protected CSanPham getItem(ResultSet rs) {
        CSanPham item = new CSanPham();
        try {
            item.setId(rs.getLong(CFields.id));
            item.setMaSanPham(rs.getString(CFields.ma_sp));
            item.setSoLgTonToiDa(rs.getInt(CFields.ton_toi_da));
            item.setTonToiThieu(rs.getInt(CFields.ton_toi_thieu));
            item.setTenSanPham(rs.getString(CFields.ten_san_pham));     
            item.setThueVat(rs.getInt(CFields.vat));
            item.setDonGia(rs.getDouble(CFields.don_gia));
            item.setGiaNhap(rs.getDouble(CFields.gia_nhap));
            item.setDonVi(rs.getString(CFields.don_vi));
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return item;
    }

    @Override
    public List<CSanPham> getAll(SubConfig config) {
        String mysql = "select * from vSan_pham " + sWhere + " order by id desc ";
        return this.getAll(mysql, config);
    }

    @Override
    public Object insertOnSubmit(CSanPham item, SubConfig config) {
        Object result = 0;
        String mysql ="insert into "+ CFields.tb_san_pham + "("+ CFields.ten_san_pham
                + "," + CFields.don_vi + "," + CFields.nguoi_tao + ","
                + CFields.ngay_tao + "," + CFields.gia_nhap + "," + CFields.ton_toi_da 
                + "," + CFields.ton_toi_thieu + "," + CFields.don_gia + ","+ CFields.vat
                + "," + CFields.ton_toi_da_ch + "," + CFields.ton_toi_thieu_ch
                + "," + CFields.ma_sp
                + ") values(?,?,?,?,?,?,?,?,?,?,?,?)";
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
            cs.setString(1, item.getTenSanPham());
            cs.setString(2, item.getDonVi());
            cs.setString(3, item.getNguoiTao());
          //  java.sql.Date ngaytao = (java.sql.Date)item.getNgayTao();
            cs.setTimestamp(4, item.getNgayTao());
          //  cs.setDate(4,  ngaytao);
            cs.setDouble(5, item.getGiaNhap());
            cs.setInt(6, item.getSoLgTonToiDa());
            cs.setInt(7, item.getTonToiThieu());
            cs.setDouble(8, item.getDonGia());
            cs.setInt(9, item.getThueVat());
            cs.setInt(10, item.getTonToiDaCH());
            cs.setInt(11, item.getTonToiThieuCH());
            cs.setString(12, item.getMaSanPham());
            cs.executeUpdate();
            ResultSet keyResultSet = cs.getGeneratedKeys();
            if (keyResultSet.next()) {
                result = (long) keyResultSet.getInt(CFields.id);   
            }            
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        
        return result;
    }

    private void updateKho(String mysql,int ton_toi_thieu,int ton_toi_da,long sp_id,SubConfig config) throws SQLException{
        PreparedStatement cs = config.connec.prepareStatement(mysql);
        cs.setInt(1,ton_toi_thieu);
        cs.setInt(2,ton_toi_da);
        cs.setLong(3, sp_id);
        cs.executeUpdate();
    }
    @Override
    public Object updateOnSubmint(CSanPham item, SubConfig config) {
       Object result = 0;
       String mysql  ="update " + CFields.tb_san_pham + " set "
               + CFields.ten_san_pham + "=?," + CFields.ton_toi_da + "=?,"
               + CFields.ton_toi_thieu + "=?,"+ CFields.don_gia + "=?,"
               + CFields.gia_nhap + "=?,"+ CFields.ton_toi_da_ch + "=?,"
               + CFields.ton_toi_thieu_ch
                + "=? where " + CFields.id + " =?";
        try {
            PreparedStatement cs = config.connec.prepareStatement(mysql);
            cs.setString(1, item.getTenSanPham());
            cs.setInt(2, item.getSoLgTonToiDa());
            cs.setInt(3, item.getTonToiThieu());
            cs.setDouble(4, item.getDonGia());
            cs.setDouble(5, item.getGiaNhap());
            cs.setInt(6, item.getTonToiDaCH());
            cs.setInt(7, item.getTonToiThieuCH());
            cs.setLong(8, item.getId());
            result = cs.executeUpdate();
            
            mysql ="update ton_kho set ton_toi_thieu =? ,ton_toi_da = ? where san_pham_id =? and kho_id in(select id from kho where cua_hang=true)";
            this.updateKho(mysql, item.getTonToiThieuCH(), item.getTonToiDaCH(), item.getId(), config);
            mysql ="update ton_kho set ton_toi_thieu =? ,ton_toi_da = ? where san_pham_id =? and kho_id in(select id from kho where cua_hang=false)";
            this.updateKho(mysql, item.getTonToiThieu(), item.getSoLgTonToiDa(), item.getId(), config);
            
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
       return result;
    }

    @Override
    public Object deleteOnSubmint(CSanPham item, SubConfig config) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

   

    @Override
    public CSanPham Find(CSanPham item, List<CSanPham> list) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public CSanPham add(CSanPham item) {
        try {
            
            ps.setString(1, item.getTenSanPham());
            ps.setString(2, item.getDonVi());
            ps.setString(3, item.getNguoiTao());
          //  java.sql.Date ngaytao = (java.sql.Date)item.getNgayTao();
            ps.setTimestamp(4, item.getNgayTao());
          //  cs.setDate(4,  ngaytao);
            ps.setDouble(5, item.getGiaNhap());
            ps.setInt(6, item.getSoLgTonToiDa());
            ps.setInt(7, item.getTonToiThieu());
            ps.setDouble(8, item.getDonGia());
            ps.setInt(9, item.getThueVat());
            ps.setString(10, item.getMaSanPham());
            ps.addBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return item;
    }

    @Override
    public void saveBatch() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public int addrows(List<CSanPham> list, SubConfig config) {
          String mysql ="insert into "+ CFields.tb_san_pham + "("+ CFields.ten_san_pham
                + "," + CFields.don_vi + "," + CFields.nguoi_tao + ","
                + CFields.ngay_tao + "," + CFields.gia_nhap + "," + CFields.ton_toi_da 
                + ","+ CFields.ton_toi_thieu + "," + CFields.don_gia + "," 
                + CFields.vat + "," + CFields.ma_sp
                + ") values(?,?,?,?,?,?,?,?,?,?)";
          int rs = 0;
        try {
            ps = config.connec.prepareStatement(mysql,PreparedStatement.RETURN_GENERATED_KEYS);
             for(CSanPham item : list){
                this.add(item);
            }
           int []n = ps.executeBatch();
        } catch (SQLException ex) {
            Logger.getLogger(CSanPhamDAO.class.getName()).log(Level.SEVERE, null, ex);
        }
        return rs;
    }

    @Override
    protected void add(CSanPham item, int t) {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

  
    
}
